(function($, doc, room) {
	mui.init();
	var settings = app.getSettings();
	var state = app.getState();
	var uid = state.user_id;
	var cdata = sysphone.getFriendPin();
	//console.log(cdata);
	var list = document.getElementById('list');
	var header = document.querySelector('.mui-bar');
	var bodyheight = document.body.offsetHeight;
	//calc hieght
	list.style.height = (bodyheight - 80) + 'px';
	window.indexedList = new mui.IndexedList(list);
	$.plusReady(function() {
		var roomId = plus.webview.currentWebview().roomId;
		var roomdata = room.get(roomId);
		var list = roomdata.list;
		var fids = {};
		for(var i in list) {
			fids[list[i]['user_id']] = list[i]['user_id'];
		}
		//console.log(JSON.stringify(roomdata));
		for(var p in cdata) {
			var pdata = cdata[p];
			for(var j = 0; j < pdata.length; j++) {
				if(pdata[j]['f_id'] in fids) {
					cdata[p][j]['checked'] = 1;
					cdata[p][j]['avatar'] = app.avatar(cdata[p][j]['user_id'], '../../static/images/addfriend_py.png');
				}
			}
		}
		//console.log(JSON.stringify(cdata));
		doc.querySelector("#list-area").innerHTML = template('list-template', {
			"flist": cdata,
			"topid": roomdata['info']['top_id'],
			"uid": uid
		});

		doc.querySelector('#addbtn').addEventListener('tap', function() {
			//var cks = doc.querySelector(".cks");
			var ids = [];
			var names = [];
			var heads = [];
			$(".cks").each(function(i, el) {
				if(el.checked == true) {
					ids.push(el.getAttribute('fid'));
					names.push(el.getAttribute('title'));
					heads.push(el.getAttribute('head'));
				}
			});
			//console.log(ids);
			//console.log(names);
			//return;
			if(ids.length > 1) {
				var postdata = {
					user_id: uid,
					room_id: roomId,
					ids: ids.join(","),
					names: names.join(","),
					heads: heads.join(",")
				};
				console.log(JSON.stringify(postdata));
				$.post(apiurl + 'chat/editRoom', postdata, function(res) {
					console.log(JSON.stringify(res));
					//return;
					if(res.status > 0) {
						room.edit(res);
						var chatbox = plus.webview.getWebviewById("chatbox");
						mui.fire(chatbox, 'sysMsg', {
							sender: uid,
							content: state.name + '邀请' + res.aname.join("、") + '加入群聊',
							type: 'sys',
							sendtype : 1
						});
						var service_comfirm = plus.webview.getWebviewById("group_set.html");
						plus.webview.close(service_comfirm, "none");
						plus.webview.currentWebview().close('none');
						var mainid = localStorage.getItem('mainpage');
						var mainWin = plus.webview.getWebviewById(mainid);
						mui.fire(mainWin, 'joinroom', {ids : postdata.ids, room_id : roomId});
						//plus.webview.show('chatbox');
						//plus.webview.close('chatbox');
						//plus.webview.close('') 
						//qdata.push(res);
						//localStorage.setItem('qunlist' + uid, JSON.stringify(qdata));
						/*
						mui.openWindow({
							url: '/pages/chat/chat_index.html',
							id: '/pages/chat/chat_index.html',

							show: {
								autoShow: true,
								aniShow: 'slide-in-right',
								duration: 100
							},
							extras: {
								chatTitle: res.info.name,
								chatHeader: '../../static/images/qun.png',
								roomId: res.room_id,
								roomType: 2,
								tomsgtype: 'tip',
								tomsgsender: 'sys',
								tomsgcontent: '你邀请' + res.aname.join("、") + '加入群聊'
							}
						});*/
					} else {
						$.alert(res.msg);
					}
				});
			} else {
				if(ids.length > 0) {
					mui.openWindow({
						url: '/pages/chat/chat_index.html',
						id: '/pages/chat/chat_index.html',

						show: {
							autoShow: true,
							aniShow: 'slide-in-right',
							duration: 100
						},
						extras: {
							chatTitle: names[0],
							chatHeader: heads[0],
							roomId: ids[0],
							roomType: 1
						}
					});
				} else {
					$.alert("请选择群成员！");
				}

			}

			//console.log(names);

		});

	});

}(mui, document, room));